﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.QueryAndReport;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockInDetailEdit : Form
    {
        private StockInHeader stockInHeaderBLL;
        private StockInDetail stockInDetailBLL;

        private MeasuringUnit measuringUnitBLL;
        private Drug drugBLL;

        private string aDrugName;
        private string aSepcifications;
        private string aMeasuringUnit;
        private string aTradePrice;
        private string aRetailPrice;

        public frmStockInDetailEdit()
        {
            InitializeComponent();

            measuringUnitBLL = new MeasuringUnit();
            drugBLL = new Drug();

            //为header的相关类初始化
            stockInHeaderBLL = new StockInHeader();
            stockInHeaderBLL.StockInTypeName = new StockInType();
            stockInHeaderBLL.SupplierName = new Supplier();

            //为detail的相关类初始化
            stockInDetailBLL = new StockInDetail();
            stockInDetailBLL.DrugNum = new Drug();
        }

        private void frmStockInDetailEdit_Load(object sender, EventArgs e)
        {
           // BindMeasuringUnit();

        }


        /// <summary>
        /// 接收header和detail来加载
        /// </summary>
        /// <param name="header"></param>
        /// <param name="detail"></param>
        public void Init(StockInHeader header,StockInDetail detail,string DrugName,string Sepcifications,string MeasuringUnit,string TradePrice,string RetailPrice)
        {
            stockInHeaderBLL = header;
            stockInDetailBLL = detail;
            aDrugName = DrugName;
            aMeasuringUnit = MeasuringUnit;
            aRetailPrice = RetailPrice;
            aTradePrice = TradePrice;
            aSepcifications = Sepcifications;

            SetInfo();
        }

        /// <summary>
        /// 设置获取的各种信息
        /// </summary>
        public void SetInfo()
        {
            //设置detail
            txtAmount.Text = stockInDetailBLL.Amount.ToString();
            txtBatchNum.Text = stockInDetailBLL.BatchNum;
            txtDrugNum.Text = stockInDetailBLL.DrugNum.DrugNum.ToString();
            txtImportPrice.Text = stockInDetailBLL.ImportPrice.ToString();
            txtProductArea.Text = stockInDetailBLL.ProductedArea;
            dtpProductedDate.Value = stockInDetailBLL.ProductedDatetime;
            dtpExpiryDate.Value = stockInDetailBLL.ExpiryDatetime;
            txtQuantity.Text = stockInDetailBLL.Quantity.ToString();

            //设置header
            txtStockInDatetime.Text = Convert.ToString(stockInHeaderBLL.StockInDatetime);
            txtStockInNum.Text = stockInHeaderBLL.StockInNum;
            txtStockInType.Text = stockInHeaderBLL.StockInTypeName.StockInTypeName;
            txtSupplier.Text = stockInHeaderBLL.SupplierName.SupplierName;

            //设置药品信息
            txtDrugName.Text = aDrugName;
            txtMeasuringUnit.Text=aMeasuringUnit;
            txtRetailPrice.Text=aRetailPrice;
            txtSpecifications.Text=aSepcifications;
            txtTradePrice.Text=aTradePrice;
        }

        /// <summary>
        /// 根据输入的数量和进价来计算金额
        /// </summary>
        public void Calcluate()
        {
            float price, amount;
            long qty;

            if (txtImportPrice.Text.Trim() != "")
            {
                price = Convert.ToSingle(txtImportPrice.Text.Trim());
            }
            else
            {
                price = 0;
            }
            if (txtQuantity.Text.Trim() != "")
            {
                qty = Convert.ToInt64(txtQuantity.Text.Trim());
            }
            else
            {
                qty = 0;
            }
            amount = price * qty;
            txtAmount.Text = Convert.ToString(amount);
        }
        
        /// <summary>
        /// 根据是否填写完必须的信息来控制按钮
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            if (txtBatchNum.Text.Trim() != "" && txtDrugNum.Text.Trim() != "" && txtImportPrice.Text.Trim() != "" && txtProductArea.Text.Trim() != "" && txtQuantity.Text.Trim() != "" )
                return btnUpdate.Enabled = true;
            else
                return btnUpdate.Enabled = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            stockInDetailBLL.SetStockInDetail(GetObject());
            this.DialogResult = DialogResult.OK;
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            SetInfo();
            btnUpdate.Enabled = false;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtProductArea.Text = txtBatchNum.Text = txtQuantity.Text = txtImportPrice.Text = "";
            dtpProductedDate.Value = dtpExpiryDate.Value = DateTime.Now;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// 获取stockindetail对象
        /// </summary>
        /// <returns></returns>
        public StockInDetail GetObject()
        {
            StockInDetail detail = new StockInDetail();
            detail.DrugNum = new Drug();

            detail.Amount = Convert.ToSingle(txtAmount.Text.Trim());
            detail.BatchNum = txtBatchNum.Text.Trim();
            detail.DrugNum.DrugNum = Convert.ToInt32(txtDrugNum.Text.Trim());
            detail.ExpiryDatetime = dtpExpiryDate.Value.Date;
            detail.ImportPrice = Convert.ToSingle(txtImportPrice.Text.Trim());
            detail.ProductedArea = txtProductArea.Text.Trim();
            detail.ProductedDatetime = dtpProductedDate.Value.Date;
            detail.Quantity = Convert.ToInt32(txtQuantity.Text.Trim());
            detail.StockInNum = txtStockInNum.Text.Trim();

            return detail;
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtQuantity, '0');
            this.Check();
            this.Calcluate();
        }

        private void txtImportPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtImportPrice, '.');
            this.Check();
            this.Calcluate();
        }

        /// <summary>
        /// 获取药品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            frmDrugInfo form = new frmDrugInfo();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                txtDrugNum.Text = Convert.ToString(form.DrugBLL.DrugNum);
                txtDrugName.Text = form.DrugBLL.TradeName;
                txtSpecifications.Text = form.DrugBLL.Sepcifications;
                txtMeasuringUnit.Text = form.DrugBLL.MeasuringUnitNum.MeasuringUnitName;
                txtTradePrice.Text = Convert.ToString(form.DrugBLL.TradePrice);
                txtRetailPrice.Text = Convert.ToString(form.DrugBLL.RetailPrice);
            }
        }
    
    }
}